package com.robot.goods.controller;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.robot.goods.pojo.GoodsCategory;
import com.robot.goods.service.GoodsCategoryService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

/**
 * category控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/category")
public class CategoryJsonController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;


    /**
     * 分页+多条件查询GoodsCategory
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findGoodsCategoryBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsCategoryBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<GoodsCategory> pageList = goodsCategoryService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<GoodsCategory>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 全部GoodsCategory
     *
     * @return
     */
    @RequestMapping(value = "third/findGoodsCategory", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsCategory() {
        return JSON.toJSONString(goodsCategoryService.findAll());
    }

    /**
     * 根据ID查询GoodsCategory
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "third/findGoodsCategoryById/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsCategoryById(@PathVariable String id) {
        return JSON.toJSONString(goodsCategoryService.findById(id));
    }

    /**
     * 通过name查询GoodsCategory
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "third/findGoodsCategoryByName/{name}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsCategoryByName(@PathVariable String name) {
        return JSON.toJSONString(goodsCategoryService.findByName(name));
    }

    /**
     * 增加GoodsCategory
     *
     * @param goodsCategory
     */
    @RequestMapping(value = "third/addGoodsCategoryByGoodsCategory", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public void thirdAddGoodsCategoryByGoodsCategory(@RequestBody String goodsCategory) {
        goodsCategoryService.add(JSON.parseObject(goodsCategory, GoodsCategory.class));
    }

    /**
     * 修改GoodsCategory
     *
     * @param goodsCategory
     */
    @RequestMapping(value = "third/updateGoodsCategoryByGoodsCategory", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public void thirdUpdateGoodsCategoryByGoodsCategory(@RequestBody String goodsCategory) {
        goodsCategoryService.update(JSON.parseObject(goodsCategory, GoodsCategory.class));
    }

    /**
     * 删除GoodsCategory
     *
     * @param id
     */
    @RequestMapping(value = "third/deleteGoodsCategoryById/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public void thirdDeleteGoodsCategoryById(@PathVariable String id) {
        goodsCategoryService.deleteById(id);
    }
}
